package com.whfc.fse.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.ResultEnum;
import com.whfc.fse.dao.FseCraneDataMapper;
import com.whfc.fse.dao.FseCraneMapper;
import com.whfc.fse.dto.FseCraneDataDTO;
import com.whfc.fse.dto.FseCraneDataLogDTO;
import com.whfc.fse.dto.OpenApiFseCraneDataDTO;
import com.whfc.fse.entity.FseCrane;
import com.whfc.fse.entity.FseCraneData;
import com.whfc.fse.service.FseCraneDataService;
import com.whfc.influx.dao.FseCraneDataLogDao;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 塔机数据
 *
 * @author GuoDong_Sun
 * @date 2020/4/9
 */
@DubboService(interfaceClass = FseCraneDataService.class, version = "1.0.0", timeout = 30 * 1000)
public class FseCraneDataServiceImpl implements FseCraneDataService {
    private static final Logger logger = LoggerFactory.getLogger(FseCraneDataServiceImpl.class);

    @Autowired
    private FseCraneMapper fseCraneMapper;

    @Autowired
    private FseCraneDataMapper fseCraneDataMapper;

    @Autowired
    private FseCraneDataLogDao fseCraneDataLogDao;

    @Override
    public FseCraneDataDTO latest(Integer craneId) {
        logger.info("获取塔机最新数据,craneId:{}", craneId);
        FseCraneData fseCraneData = fseCraneDataMapper.selectByCraneId(craneId);
        FseCraneDataDTO fseCraneDataDTO = new FseCraneDataDTO();
        BeanUtils.copyProperties(fseCraneData, fseCraneDataDTO);
        return fseCraneDataDTO;
    }

    @Override
    public ListData<FseCraneDataLogDTO> log(Integer craneId, Date startTime, Date endTime) {
        logger.info("获取塔机监控的历史数据,craneId:{},startTime:{},endTime:{}", craneId, startTime, endTime);
        List<FseCraneDataLogDTO> list = fseCraneDataLogDao.selectByCraneIdAndTime(craneId, startTime, endTime);
        return new ListData<>(list);
    }

    @Override
    public ListData<FseCraneDataLogDTO> log(String sn, Date startTime, Date endTime) throws BizException {
        if ((double) (endTime.getTime() - startTime.getTime()) / 60000 > 30) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "时间范围最大为30分钟");
        }
        FseCrane fseCrane = fseCraneMapper.selectByPlatformAndSn(null, sn);
        if (fseCrane == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "硬件不存在");
        }
        List<FseCraneDataLogDTO> list = fseCraneDataLogDao.selectByCraneIdAndTime(fseCrane.getId(), startTime, endTime);
        return new ListData<>(list);
    }

    @Override
    public OpenApiFseCraneDataDTO getCraneData(String sn) throws BizException {
        FseCraneData fseCraneData = fseCraneDataMapper.selectBySn(sn);
        OpenApiFseCraneDataDTO openApiFseCraneDataDTO = new OpenApiFseCraneDataDTO();
        BeanUtils.copyProperties(fseCraneData, openApiFseCraneDataDTO);
        return openApiFseCraneDataDTO;
    }
}
